/**
 * jQuery Plugin to obtain touch gestures from iPhone, iPod Touch and iPad, should also work with Android mobile phones (not tested yet!)
 * Common usage: wipe images (left and right to show the previous or next image)
 * 
 * @author Andreas Waltl, netCU Internetagentur (http://www.netcu.de)
 * @version 1.0 (15th July 2010)
 */

(function($) { 
$.fn.touchit = function(settings) {
	// ---------------------------------------------------------------------------
	// Setup	
	var config = {
		disabled: false,
		filter: ':input,option',
		preventDefaultEvents: true,
		touchstart: false,
		touchmove: false,
		touchend: false
	};
	if (settings) $.extend(config, settings);
	var selector = this.selector;
	if (selector == '') return false;
	var touches = [];
	function touchIdentIndex(ident,touches){
		var tobj;
		for (var i in touches){
			tobj = touches.item(i);
			if (tobj.identifier == ident) {
				return i;
			}
		}		
		return -1;
	}
	function touchesIndex(ident){
		for (var i=0;i<touches.length;i++){
			if (touches[i].identifier == ident) {
				return i;
			}
		}		
		return -1;
	}	
	function touchesIndexByTag(tag){
		for (var i=0;i<touches.length;i++){
			if (touches[i].tag == tag) {
				return i;
			}
		}		
		return -1;
	}	
	function touchCnt(){
		var cnt = 0;
		for (var i=0;i<touches.length;i++){
			if (touches[i].down) {
				cnt++;
			}
		}		
		return cnt;
	}
	// ---------------------------------------------------------------------------
	// Primary Events
	function updateTouches(newtouches,e){
		if (newtouches == null || typeof newtouches == 'undefined') return false;
		var ind;
		var nt;
		var touch;
		var now = new Date();
	    var time = now.getTime();		
		var i;
		var moved = false;
		var cnt = touchCnt();
		if (newtouches.length > 0) {
			for (var i in newtouches) {
				moved = false;
				touch = newtouches.item(i);
				ind = touchesIndex(touch.identifier);
				if (ind == -1){
					nt = new touch_obj();
					nt.identifier = touch.identifier;
					nt.X = touch.pageX;
					nt.Y = touch.pageY;
					nt.startX = touch.pageX;
					nt.startY = touch.pageY;
					nt.time = time;
					nt.down = true;
					touches.push(nt);
					if (config.touchstart)
						config.touchstart(e,nt,cnt);
				} else {
					nt = touches[ind];
					if (nt.X != touch.pageX || nt.Y != touch.pageY) {
						nt.X = touch.pageX;
						nt.Y = touch.pageY;
						moved = true;
					}
					nt.time = time;
					if (moved) {
						if (config.touchmove)
							config.touchmove(e,nt,cnt);
					}
				}
			}
		}
		for (i=0;i<touches.length;i++){
			nt = touches[i];
			if (nt.down && nt.time != time){
				nt.down = false;
				// touch stopped/removed
				if (config.touchend)
					config.touchend(e,nt,cnt);
			}
		}
	}
	$('body').delegate(selector,'touchstart',function(e) {
		var ev = e.originalEvent;
		if (config.disabled) return true;
		updateTouches(ev.touches,e);
		
	});					
	$('body').delegate(selector,'touchmove',function(e) {
		var ev = e.originalEvent;
		if (config.disabled) return true;
		updateTouches(ev.touches,e);
		
	});	
	$('body').delegate(selector,'touchend touchcancel',function(e) {
		var ev = e.originalEvent;
		if (config.disabled) return true;
		updateTouches(ev.touches,e);	
		
	});	
	return this;
};
})(jQuery);

function touch_obj(){}
touch_obj.prototype = {
	identifier: -1,
	startX: 0,
	startY: 0,	
	X: 0,
	Y: 0,
	down: false,
	time: 0,
	change: 'none/move/start/end',
	tag: ''	
}